import 'package:flutter/material.dart';
import 'package:sharing/model/book.dart';
import 'package:sharing/utils/whole_apis.dart';
import 'package:sharing/ui/widget/book_info.dart';
class BookInfoWidgetItem extends StatelessWidget {
  final Book book;

  BookInfoWidgetItem({@required this.book}) : assert(book != null);

  @override
  Widget build(BuildContext context) {
    print("build");
    return GestureDetector(
      onTap: (){
        Navigator.of(context).push(MaterialPageRoute(builder: (context){
          return BookInfo(book: book,);
        }));
      },
      child: Container(
        //height: 300,
        child: Card(

          margin:
          const EdgeInsets.only(left: 4.0, right: 4.0, top: 4.0, bottom: 4.0),
          elevation: 4.0,
          child: Column(
            mainAxisAlignment: MainAxisAlignment.start,
            crossAxisAlignment: CrossAxisAlignment.start,
            children: <Widget>[
              // 一个列, 用户头像, 用户名, 发布的日期
              _buildHeader(context),
              // 显示标题
              _buildTitle(context),
              // 文章的图片
              _buildBody(context),

              _buildFooter(context),
            ],
          ),
        ),
      ),
    );

  }

  Widget _buildHeader(BuildContext context) {
    return Row(
      mainAxisAlignment: MainAxisAlignment.start,
      crossAxisAlignment: CrossAxisAlignment.center,
      children: <Widget>[
        Container(
          margin: const EdgeInsets.only(
              left: 8.0, top: 4.0, right: 4.0, bottom: 0.0),
          child: CircleAvatar(
            radius: 18,
            backgroundImage: NetworkImage(
                "https://avatars1.githubusercontent.com/u/46526052?s=400&u=b0f64fc2f67c45c17341d0090120d1e23f6b6478&v=4"),
          ),
        ),
        Expanded(
          child: Container(
            padding: const EdgeInsets.only(left: 8.0),
            child: Text(
              book.author,
              style: TextStyle(
                fontSize: 14.0,
                color: Colors.black,
              ),
            ),
          ),
        ),
        Container(
          padding: const EdgeInsets.only(left: 8.0, right: 8.0),
          child: Text(
            book.dateTime.toString(),
            //字体的样式
            style: TextStyle(
                fontSize: 14.0,
                fontWeight: FontWeight.normal,
                color: Color(0xff868d9e)),
          ),
        ),
      ],
    );
  }

  Widget _buildTitle(BuildContext context) {
    return Container(
      padding:
          const EdgeInsets.only(left: 16.0, right: 16.0, top: 4.0, bottom: 8.0),
      child: Text(
        book.name,
        maxLines: 2,
        overflow: TextOverflow.ellipsis,
        style: TextStyle(fontSize: 14.0, fontWeight: FontWeight.normal),
      ),
    );
  }

  Widget _buildBody(BuildContext context) {
    print("cover ${book.cover}");
    return Container(
      height: 140.0,
      width: MediaQuery.of(context).size.width,
      child: Container(
        padding: const EdgeInsets.symmetric(horizontal: 4.0),
        child: Image.network(
          book.cover != null
              ? WholeApis.hostName +"/"+ book.cover
              : "https://gss1.bdstatic.com/9vo3dSag_xI4khGkpoWK1HF6hhy/baike/w%3D268%3Bg%3D0/sign=e85dd07b7f094b36db921ceb9bf71be4/0b55b319ebc4b7459a5999b2c6fc1e178b821540.jpg",
          fit: BoxFit.cover,
        ),
      ),
    );
  }

  _buildFooter(BuildContext context) {
    return Container(

      padding:
          const EdgeInsets.only(left: 8.0, right: 8.0, top: 4.0, bottom: 8.0),
      child: Row(
        crossAxisAlignment: CrossAxisAlignment.center,
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: <Widget>[
          Container(
            child: Text("20人看过"),
          ),
          Container(
            padding: const EdgeInsets.only(left: 8.0),
            child: Text("分类标签" + book.type == null ? "" : book.type),
          ),

         Expanded(
           child: Container(),
         ),
        ],
      ),
    );
  }
}
